<template>
  <div class="list">
    <div class="header">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="formInline.id"
            placeholder="请输入商品ID"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="formInline.goodsSn"
            placeholder="请输入商品编号"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="formInline.name"
            placeholder="请输入商品名称"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="onSubmit"
          >查找</el-button>
          <el-button
            type="primary"
            class="el-icon-edit"
            @click="handleAdd"
          >添加</el-button>
          <el-button
            type="primary"
            class="el-icon-download"
            @click="handleDaochu"
          >导出</el-button>
          <el-button
            type="danger"
            class="el-icon-delete"
            :disabled="!hasSelection"
            @click="batchDelete"
          >批量删除</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="main">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品编号">
                <span>{{ props.row.goodsSn }}</span>
              </el-form-item>
              <el-form-item label="宣传画廊">
                <img
                  v-for="(item, index) in props.row.gallery"
                  :key="index"
                  :src="item"
                  alt=""
                >
              </el-form-item>
              <el-form-item label="商品介绍">
                <span>{{ props.row.brief }}</span>
              </el-form-item>
              <el-form-item label="商品单位">
                <span>{{ props.row.unit }}</span>
              </el-form-item>
              <el-form-item label="keywords">
                <span>{{ props.row.category }}</span>
              </el-form-item>
              <el-form-item label="类目ID">
                <span>{{ props.row.categoryId }}</span>
              </el-form-item>
              <el-form-item label="品牌商ID">
                <span>{{ props.row.brandId }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="商品ID" align="center" />
        <el-table-column prop="name" label="名称" align="center" />
        <el-table-column prop="picUrl" label="图片" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.picUrl">
              <img
                :src="scope.row.picUrl"
                alt=""
                style="width: 40px; height: 40px"
              >
            </div>
            <div v-else>
              <el-tag type="info">加载失败</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="shareUrl" label="分享图" align="center" />
        <el-table-column label="详情" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleView(scope.row)"
            >查看</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="counterPrice" label="市场售价" align="center" />
        <el-table-column prop="retailPrice" label="当前价格" align="center" />
        <el-table-column prop="isNew" label="是否新品" align="center">
          <template slot-scope="scope">
            <p v-if="scope.row.isNew">
              <el-tag type="success">新品</el-tag>
            </p>
            <p v-else>
              <el-tag>非新品</el-tag>
            </p>
          </template>
        </el-table-column>
        <el-table-column prop="isHot" label="是否热品" align="center">
          <template slot-scope="scope">
            <p v-if="scope.row.isHot">
              <el-tag type="success">热品</el-tag>
            </p>
            <p v-else>
              <el-tag>非热品</el-tag>
            </p>
          </template>
        </el-table-column>
        <el-table-column prop="isOnSale" label="是否在售" align="center">
          <template slot-scope="scope">
            <p v-if="scope.row.isOnSale">
              <el-tag type="success">在售</el-tag>
            </p>
            <p v-else>
              <el-tag>未售</el-tag>
            </p>
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          align="center"
          width="200px"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.row)"
            >编辑</el-button>

            <el-button
              size="mini"
              type="danger"
              @click="handleDel(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
      <!-- 对话框 -->
      <el-dialog title="商品详情" :visible.sync="dialogVisible" width="60%">
        <div v-html="currentDetail" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { listGoods, deleteGoods } from '@/api/goods.js'
import pagination from '@/components/Pagination/index.vue'
import * as XLSX from 'xlsx'
import { saveAs } from 'file-saver'
export default {
  // 标识,当前组件的名称
  name: 'List',
  components: {
    pagination
  },
  data() {
    return {
      total: 0,
      formInline: {
        id: null,
        goodsSn: null,
        name: null
      },
      tableData: [],
      multipleSelection: [],
      hasSelection: false, // 控制删除按钮是否禁用的标志
      dialogVisible: false,
      currentDetail: '',
      listQuery: {
        page: 1,
        limit: 20,
        sort: 'add_time',
        order: 'desc'
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      listGoods(this.listQuery)
        .then((response) => {
          this.tableData = response.data.data.list
          this.total = response.data.data.total
        })
        .catch(() => {
          this.tableData = []
          this.total = 0
        })
    },
    // 导出
    handleDaochu() {
      // 创建一个workbook
      const workbook = XLSX.utils.book_new() // 导出数据
      const worksheet = XLSX.utils.json_to_sheet(this.tableData)
      XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet 1') // 将workbook转为二进制数据

      const excelData = XLSX.write(workbook, {
        type: 'array',
        bookType: 'xlsx'
      }) // 创建blob对象并保存excel文件
      const blob = new Blob([excelData], { type: 'application/octet-stream' }) // 设置导出文件名字
      saveAs(blob, 'data.xlsx')
    },
    // 查找
    onSubmit() {
      this.listQuery.goodsSn = this.formInline.goodsSn
      this.listQuery.goodsId = this.formInline.id
      this.listQuery.name = this.formInline.name
      this.getList()
    },
    // 查看
    handleView(row) {
      this.currentDetail = row.detail
      this.dialogVisible = true
    },
    // 编辑
    handleEdit(row) {
      this.$router.push({
        path: 'create',
        query: {
          row: row
        }
      })
    },
    // 删除
    handleDel(row) {
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          deleteGoods(row).then((res) => {
            console.log(res)
            this.getList()
          })
        })
        .catch(() => {})
      console.log(row)
    },
    // 多选框
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.hasSelection = this.multipleSelection.length > 0
    },
    // 批量删除
    batchDelete() {
      for (let i = 0; i < this.multipleSelection.length; i++) {
        deleteGoods(this.multipleSelection[i]).then((res) => {
          this.getList()
        })
      }
    },
    // 添加
    handleAdd() {
      this.$router.push({
        path: 'create',
        query: {}
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "./scss/list.scss";
</style>
